<?php

header('Content-Type: text/html; charset=utf-8');
error_reporting(0);
include_once 'DatabaseHelper.php';
require_once 'template.php';
//Start session
session_start();
if (!isset($_SESSION['SESS_MEMBER_ID']) || (trim($_SESSION['SESS_MEMBER_ID']) == '')) {
    die("Vui lòng đăng nhập để thực hiện chức năng này!! <br/>
        <a href='index.php'> Trở về trang chủ </a>");
}

//case close/open comment
if (isset($_POST['close_comment']) || isset($_POST['open_comment'])) {
    $post_id = $_POST['postid'];
    $update  = "UPDATE post SET hidecomment =" . ($_POST['close_comment'] ? "1" : "0") . " WHERE id='$post_id'";
    DatabaseHelper::getInstance()->connect();
    DatabaseHelper::getInstance()->query($update);
    DatabaseHelper::getInstance()->close();
    header("location: index.php?id=$post_id#comment");
    
    return;
    
}

//case redraft post
if (isset($_POST['redraft'])) {
    $post_id = $_POST['postid'];
    $update  = "UPDATE post SET isdraft = 1 WHERE id= $post_id";
    DatabaseHelper::getInstance()->connect();
    DatabaseHelper::getInstance()->query($update);
    DatabaseHelper::getInstance()->close();
    header("location: draft.php");
    return;
}

//case edit draft or post
if (isset($_POST['edit_draft']) || isset($_POST['editpost'])) {
    
    $edit_post_id = $_POST['postid'];
    $post_query   = "SELECT * FROM post WHERE id='$edit_post_id' limit 1";
    $title        = "";
    $entry        = "";
    
    DatabaseHelper::getInstance()->connect();
    $post_result = DatabaseHelper::getInstance()->query($post_query);
    
    while ($post_row = mysql_fetch_array($post_result)) {
        $cate_id = $post_row['category'];
        $title   = stripslashes($post_row['title']);
        $entry   = stripslashes($post_row['entry']);
    }
    
    $header     = new Page('template/header-tpl.php');
    $adminPanel = new Page('template/admin-login.php');
    $slidebar   = new Page('template/slidebar-editor-post-tpl.php');
    $slidebar->replace_tags(array(
        "admin_area" => $adminPanel->page
    ));
    
    $mainpage = new Page('template/index-tpl.php');
    $footer   = new Page('template/footer-tpl.php');
    
    $post_editor = new Page('template/post-editor-tpl.php');
    $post_editor->replace_tags(array(
        "post_id" => $edit_post_id
    ));
    
    $combox      = new Page('template/category-combox-tpl.php');
    
    //create combox category here
    //<option value="other">Khác, mục mới</option>
    DatabaseHelper::getInstance()->connect();
    $query  = "SELECT * FROM category";
    $result = DatabaseHelper::getInstance()->query($query);
    DatabaseHelper::getInstance()->close();
    
    $row_cate_combobox = "";
    
    while ($row = mysql_fetch_array($result)) {
        $id       = $row['category_id'];
        $cateName = $row['category_name'];
        
        $seleted_attribute = "";
        
        if ($id == $cate_id) {
            $seleted_attribute = "selected='selected'";
        }
        
        $row_cate_combobox .= "<option " . $seleted_attribute . " value='$id'>$cateName</option>";
    }
    
    DatabaseHelper::getInstance()->close();
    $combox->replace_tags(array(
        "category_choices" => $row_cate_combobox
    ));
    
    $post_editor->replace_tags(erray(
        "category_combo_box" => $combox->page,
        "post_title" => $title,
        "post_content" => $entry
    ));
    
    
    $mainpage->replace_tags(array(
        "header" => $header->page,
        "main_content" => $post_editor->page,
        "slidebar" => $slidebar->page,
        "footer" => $footer->page,
        "page_nav" => "",
        "comment_separate" => "",
        "comment" => "",
        "comment_form" => "",
        "category_name" => ""
    ));
    
    $mainpage->output();
    
    return;
}

//case delete draft
if (isset($_POST['delete_draft'])) {
    $del_post_id  = $_POST['postid'];
    $delete_query = "DELETE FROM post where id ='$del_post_id' ";
    DatabaseHelper::getInstance()->connect();
    DatabaseHelper::getInstance()->query($delete_query);
    DatabaseHelper::getInstance()->close();
    header("location: draft.php");
    
    return;
}

//case publish draft
if (isset($_POST['publish_draft'])) {
    $publish_post_id = $_POST['postid'];
    
    $update_query = "UPDATE post SET isDraft = '0' where id ='$publish_post_id' ";
    DatabaseHelper::getInstance()->connect();
    DatabaseHelper::getInstance()->query($update_query);
    DatabaseHelper::getInstance()->close();
    
    header("location: index.php");
    return;
}


if (isset($_POST['submit']) || isset($_POST['savedraft'])) {
    
    $date      = new DateTime();
    $timestamp = $date->getTimestamp();
    $title     = htmlspecialchars(strip_tags($_POST['title']));
    $entry     = $_POST['wysiwyg'];
    $entry     = nl2br($entry);
    
    if (!get_magic_quotes_gpc()) {
        $title = addslashes($title);
        $entry = addslashes($entry);
    }
    $cate_ID = $_POST['choose_category'];
    
    DatabaseHelper::getInstance()->connect();
    if ($cate_ID == 'other') {
        $new_category = htmlspecialchars(strip_tags($_POST['choice_new_category']));
        $insertQuery  = "INSERT INTO category (category_id, category_name) 
                    VALUES (NULL,'$new_category')";
        echo "NEW category : $new_category";
        $cate_ID = DatabaseHelper::getInstance()->getIncrement('category');
        $result  = DatabaseHelper::getInstance()->query($insertQuery);
    }
    $isDraft = isset($_POST['submit']) ? '0' : '1';
    
     if(isset($_POST['postid']) && ($_POST['postid'] != "{post_id}"))
     {
         $publish_post_id = $_POST['postid'];
         $sql = "UPDATE post SET timestamp = '$timestamp',title = '$title',entry = '$entry',category = '$cate_ID',isdraft = '$isDraft' WHERE id = $publish_post_id";
         
     } else 
     {
         $sql = "INSERT INTO post (timestamp,title,entry,category,isdraft) VALUES ('$timestamp','$title','$entry','$cate_ID','$isDraft')";         
         
     }

    DatabaseHelper::getInstance()->query($sql);
    DatabaseHelper::getInstance()->close();
    
    if (isset($_POST['submit'])) {
        header("location: index.php");
    }
    
    if (isset($_POST['savedraft'])) {
        header("location: draft.php");
    }
    
    return;
} 

    $header = new Page('template/header-tpl.php');
    
    //slide bar dependon login or not login
    $adminPanel = new Page('template/admin-login.php');
    $slidebar   = new Page('template/slidebar-editor-post-tpl.php');
    $slidebar->replace_tags(array(
        "admin_area" => $adminPanel->page
    ));
    
    $mainpage = new Page('template/index-tpl.php');
    $footer   = new Page('template/footer-tpl.php');
    
    $post_editor = new Page('template/post-editor-tpl.php');
    $combox      = new Page('template/category-combox-tpl.php');
    
    //create combox category here
    //<option value="other">Khác, mục mới</option>
    DatabaseHelper::getInstance()->connect();
    $query  = "SELECT * FROM category";
    $result = DatabaseHelper::getInstance()->query($query);
    
    $row_cate_combobox = "";
    
    while ($row = mysql_fetch_array($result)) {
        $id       = $row['category_id'];
        $cateName = $row['category_name'];
        $row_cate_combobox .= "<option value='$id'>$cateName</option>";
    }
    
    DatabaseHelper::getInstance()->close();
    $combox->replace_tags(array(
        "category_choices" => $row_cate_combobox
    ));
    
    $post_editor->replace_tags(array(
        "category_combo_box" => $combox->page,
        "post_title" => "",
        "post_content" => ""
    ));
    
    
    $mainpage->replace_tags(array(
        "header" => $header->page,
        "main_content" => $post_editor->page,
        "slidebar" => $slidebar->page,
        "footer" => $footer->page,
        "page_nav" => "",
        "comment_separate" => "",
        "comment" => "",
        "comment_form" => "",
        "category_name" => ""
    ));
    $mainpage->output();

?>